package com.glodon.paas.qto.util;

import com.glodon.paas.account.api.oauth1.AccessValidationResponse;
import com.glodon.paas.account.sdk.oauth1.web.OAuthFilter;
import com.glodon.paas.exception.BaseErrorException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import javax.servlet.http.HttpServletRequest;


public class SecurityUtils {

    public static String getOwnerId(HttpServletRequest request) throws BaseErrorException{
    	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            if (((OAuth2Authentication) authentication).getUserAuthentication() != null) {
                return (String) ((OAuth2Authentication) authentication).getUserAuthentication().getPrincipal();
            }
            return null;
        }
    	AccessValidationResponse response = OAuthFilter.getValidationResponse(request);
		return response.getUserId();
    }
    
    
}
